package com.lxf.complayTest.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.lxf.complayTest.service.UserService;
import com.lxf.complayTest.dao.UserMapper;
import com.lxf.complayTest.pojo.Emply;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Luoxingfa on 2023/2/28 0028 17:47
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    /**
     * 获取数据
     */
    @Override
    public JSONObject getUserList(JSONObject json, HttpServletRequest request) {
        JSONObject js = new JSONObject();
        try {
            String uName = json.getString("uName");  //搜索参数
            Integer page = json.getInteger("page");  //页数
            Integer limit = json.getInteger("limit");//限制
            Integer startPage = (page-1)*limit;             //初始页
            /**
             * 查询数据
             */
            List<Emply> list = userMapper.getUserByParams(uName,startPage,limit);
            js.put("code",200);
            js.put("msg","数据获取成功！");
            js.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            js.put("code",401);
            js.put("msg","发生异常！");
            js.put("异常信息为：",e.getMessage());
        }
        return js;
    }

    /**
     * 新增
     * @param json
     * @param request
     * @return
     */
    @Override
    public JSONObject addUser(JSONObject json, HttpServletRequest request) {
        JSONObject js = new JSONObject();
        try {
            List<Emply> list = new ArrayList<>();
            //new一个实体，用于装载
            Emply user = new Emply();
            //获取传输过来的数据
            String uName = json.getString("uName");
            String uAge = json.getString("uAge");
            String uSex = json.getString("uSex");
            String uPhone = json.getString("uPhone");
            String uAddress = json.getString("uAddress");
            //将数据装载入实体
            user.setUName(uName);
            user.setUAge(uAge);
            user.setUSex(uSex);
            user.setUAddress(uAddress);
            //将实体加入集合，新增数据
            list.add(user);
            int flag = userMapper.addUser(list);
            if (flag>0){
                js.put("code",200);
                js.put("msg","数据新增成功！");
            }else {
                js.put("code",402);
                js.put("msg","数据新增失败，请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            e.printStackTrace();
            js.put("code",401);
            js.put("msg","发生异常！");
            js.put("异常信息为：",e.getMessage());
        }
        return js;
    }

    /**
     * 根据id修改信息
     * @param json
     * @param request
     * @return
     */
    @Override
    public JSONObject updateUserById(JSONObject json, HttpServletRequest request) {
        JSONObject js = new JSONObject();
        try {
            Integer id = json.getInteger("id");
            //需要修改的参数
            String uName = json.getString("uName");
            String uSex = json.getString("uSex");
            String uPhone = json.getString("uPhone");
            String uAddress = json.getString("uAddress");
            //根据id获取信息
            Emply user = userMapper.getUserById(id);
            //为空判断
            if (uName==null||uName.equals("")) uName = new String(user.getUName());
            if (uSex==null ||uSex.equals("")) uSex = new String(user.getUSex());
            if (uPhone==null||uPhone.equals(""))uPhone = new String(user.getUPhone());
            if (uAddress ==null||uAddress.equals(""))uAddress = new String(user.getUAddress());

            int flag = userMapper.updateUser(id,uName,uSex,uPhone,uAddress);
            if (flag>0){
                js.put("code",200);
                js.put("msg","数据更新成功！");
            }else {
                js.put("code",402);
                js.put("msg","数据更新失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            e.printStackTrace();
            js.put("code",401);
            js.put("msg","发生异常！");
            js.put("异常信息为：",e.getMessage());
        }
        return js;
    }

    /**
     * 事务回滚
     * @param json
     * @param request
     * @return
     */
    @Override
    public JSONObject rollBack(JSONObject json, HttpServletRequest request) {
        JSONObject js = new JSONObject();
        Connection con = null;
        try {
            Integer parms = json.getInteger("parms");//参数为1，则回滚
            if (parms==1){
                con.rollback();//回滚之前的操作
            }
        }catch (Exception e){
            e.printStackTrace();
            e.printStackTrace();
            js.put("code",401);
            js.put("msg","发生异常！");
            js.put("异常信息为：",e.getMessage());
        }
        return js;
    }
}
